// pages/category/index.js

import { request } from "../../request/index.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
     categoryList:[],
     choseIndex:0,
     //右侧的商品数据
     shopList:[]
  },

  //全局数据
  Cates:[],
  
  onLoad:function(options){

    //数据缓存
    const  cates =wx.getStorageSync('cates');
    if(!cates){
      this.getCategoryList();
    }else{
      //判断过期时间
      if(Date.now()-cates.time>1000*1000){
        this.getCategoryList();
      }else{
        this.Cates =cates.data;
        let categoryList = this.Cates;
        let  shopList =this.Cates[0].children;
       this.setData({
         categoryList,
         shopList
       })
      }
    }
  },

  getCategoryList(){
    request({url:'https://api-hmugo-web.itheima.net/api/public/v1/categories'})
    .then(result=>{
      this.Cates =result.data.message;
      //吧接口的数据存到本地
      wx.setStorageSync('cates', {time:Date.now(),data:this.Cates});
      let categoryList = this.Cates;
      let  shopList =result.data.message[0].children;
       this.setData({
         categoryList,
         shopList
       })
    })
  },
  //左侧事件的点击时间
  handleItemTap(e){
    console.log(e.currentTarget.dataset);
    const {index} =e.currentTarget.dataset;
    let shopList = this.Cates[index].children;
    this.setData({
      choseIndex:index,
       shopList
    })
  }
})